package com.gimi.cloud.bbp.manage.consumer.service.prod.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.prod.EcProdCategoryDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.EcProdCategoryFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.prod.EcProdCategoryService;

@Service
public class EcProdCategoryServiceImpl implements EcProdCategoryService {
    private static final Logger logger = LoggerFactory.getLogger(EcProdCategoryServiceImpl.class);
    private final EcProdCategoryFeignService prodCategoryFeignService;

    @Autowired
    public EcProdCategoryServiceImpl(EcProdCategoryFeignService prodCategoryFeignService){
        this.prodCategoryFeignService = prodCategoryFeignService;
    }

    @Override
    public Response getCategoryList(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) {
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        return prodCategoryFeignService.getCategoryList(prodCategoryDTO);
    }

    @Override
    public Response addCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        if(StringUtil.isNotBlank(prodCategoryDTO.getProdCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"品类Id不为空，请检查参数");
        }
        if(StringUtil.isBlank(prodCategoryDTO.getParentCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择父品类");
        }
        if(StringUtil.isBlank(prodCategoryDTO.getProdCatName())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"品类名称不能为空");
        }
        if(null == prodCategoryDTO.getCatGrade()){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择分类级别");
        }
        int parentLength = prodCategoryDTO.getParentCatId().length();
        if(prodCategoryDTO.getCatGrade()==1 ){
            if( parentLength !=1){
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else if (prodCategoryDTO.getCatGrade()==2 ){
            if( parentLength !=2) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else {
            if( parentLength !=5) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }
        return prodCategoryFeignService.saveOrUpdate(prodCategoryDTO);
    }

    @Override
    public Response updateCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getProdCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"品类Id为空，更新失败");
        }
        if(StringUtil.isBlank(prodCategoryDTO.getParentCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择父品类");
        }
        if(StringUtil.isBlank(prodCategoryDTO.getProdCatName())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"品类名称不能为空");
        }
        if(null == prodCategoryDTO.getCatGrade()){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"请选择分类级别");
        }
        int parentLength = prodCategoryDTO.getParentCatId().length();
        if(prodCategoryDTO.getCatGrade()==1 ){
            if( parentLength !=1){
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else if (prodCategoryDTO.getCatGrade()==2 ){
            if( parentLength !=2) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else {
            if( parentLength !=5) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }
        return prodCategoryFeignService.saveOrUpdate(prodCategoryDTO);
    }

    @Override
    public Response deleteCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) {
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        if(prodCategoryDTO==null || StringUtil.isBlank(prodCategoryDTO.getProdCatId()) || StringUtil.isBlank(prodCategoryDTO.getIsDelete())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.categoryDelete(prodCategoryDTO);
    }

    @Override
    public Response getCategoryPageList(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        return prodCategoryFeignService.getCategoryPageList(prodCategoryDTO);
    }

    @Override
    public Response gotoEditProdCategory(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,EcProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getProdCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.gotoEditProdCategory(prodCategoryDTO);
    }

    @Override
    public Response searchCategoryListByGrade(String requestData, HttpServletRequest request, HttpServletResponse response) {
        EcProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestData,EcProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getCatGrade())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.searchCategoryListByGrade(prodCategoryDTO);
    }

	@Override
	public Response iconUpload(HttpServletRequest request, HttpServletResponse response) {
		StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
		return prodCategoryFeignService.iconUpload(multipartHttpServletRequest.getMultiFileMap());
	}
    
}
